package com.gimi.cloud.bbp.client.service.userInfo.impl;

import com.gimi.cloud.bbp.dao.dao.User.UserInfoDao;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gimi.cloud.bbp.client.service.userInfo.UserInfoService;
import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.exception.MsgException;
import com.gimi.cloud.bbp.common.utils.reqhead.Response;
import com.gimi.cloud.bbp.dto.userInfo.RdUserInfoDTO;
import com.gimi.cloud.bbp.entity.userInfo.RdUserInfoEntity;

import tk.mybatis.mapper.entity.Example;
@Service
@Transactional
public class UserInfoServiceImpl implements UserInfoService {
	
	@Autowired
	private UserInfoDao userInfoDao;

	@Override
	public void addUserInfo(RdUserInfoEntity userInfoDTO) {
		RdUserInfoEntity record = new RdUserInfoEntity();
		BeanUtils.copyProperties(userInfoDTO, record);
		int i = userInfoDao.insert(record);
		if (i <= 0) {
			throw new MsgException(ResponseCodeEnum.INVALID);
		}
	}
	
	@Override
	public RdUserInfoDTO getUserInfoSingle(RdUserInfoDTO userInfoDTO) {
		RdUserInfoEntity userInfoEntity = userInfoDao.selectByUserId(userInfoDTO.getUserId());
		RdUserInfoDTO dto = new RdUserInfoDTO();
		BeanUtils.copyProperties(userInfoEntity, dto);
		return dto;
	}

	@Override
	public void updateUserInfo(RdUserInfoDTO userInfoDTO) {
		RdUserInfoEntity record = new RdUserInfoEntity();
		BeanUtils.copyProperties(userInfoDTO, record);
		Example example = new Example(RdUserInfoEntity.class);
		example.createCriteria().andEqualTo("userId", userInfoDTO.getUserId());
		List<RdUserInfoEntity> selectByExample = userInfoDao.selectByExample(example);
		if (selectByExample==null||selectByExample.size()<1) {
			userInfoDao.baseInsert(record);
		}else {
			userInfoDao.updateByExampleSelective(record, example);
		}
	}
	
	@Override
	public void updateUserInfo(RdUserInfoEntity userIdinfo) {
		int i = userInfoDao.updateByPrimaryKeySelective(userIdinfo);
		if (i <= 0) {
			throw new MsgException(ResponseCodeEnum.INVALID);
		}
	}

	@Override
	public RdUserInfoEntity getUserInfoByUserId(Integer userId) {
		return userInfoDao.selectByUserId(userId);
	}

	@Override
	public RdUserInfoDTO getUserBycardId(String cardId) {
		RdUserInfoEntity userInfoEntity = userInfoDao.selectByCardId(cardId);
		if(userInfoEntity!=null){
			RdUserInfoDTO dto = new RdUserInfoDTO();
			BeanUtils.copyProperties(userInfoEntity, dto);
			return dto;
		}
		return null;
	}

	@Override
	public Response updateUnimportantUserinfo(RdUserInfoDTO userInfoDTO) {
		Integer userId = userInfoDTO.getUserId();
		RdUserInfoEntity userinfo = userInfoDao.selectByUserId(userId);
		if (userinfo==null) {
			userinfo = new RdUserInfoEntity();
			userinfo.setUserId(userId);
		}
		userinfo.setNick(userInfoDTO.getNick());
		userInfoDao.updateByPrimaryKeySelective(userinfo);
		return new Response(ResponseCodeEnum.SUCCESSED,"保存成功");
	}

}
